package com.shop2cn.iapi.agentproducts.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.AutoImportProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.shop2cn.iapi.agentproducts.service.AutoImportProductCall;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.SetMarkupConfigBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.SetMarkupConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.MarkupConfig;
import com.ymttest.database.sqlwapper.godmch.MarkupconfigWapper;

/**
 * 自动导入商品Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AutoImportProduct {
	private static AutoImportProductBean autoimportproductBean;
	private static AutoImportProductCall autoimportproductCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("自动导入商品Test");
	}

	@Before
	public void caseUp() {
		autoimportproductBean = new AutoImportProductBean();
		autoimportproductCall = new AutoImportProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(autoimportproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AutoImportProduct_001() {
		Logger.start(true, "自动导入--打标，然后验证价格、正确");
		try {
			
			// 创建渠道商
			CreateCodeBean createAllMsgSellerMch = GodmchManageCallService.CreateAllMsgSellerMch();
			// 创建渠道商邀请码
			CreateCodeCall createCodeCall2 = GodmchManageCallService.createCodeCall(createAllMsgSellerMch);
			// 创建代理商
			Integer agentUserId = GodmchManageCallService.CreateSellerMch(
					createCodeCall2.getJsonObject().getAsJsonObject("result").get("code").getAsString());
			int rootUserId = createAllMsgSellerMch.getMchId();

			SetMarkupConfigBean bean = GodmchManageCallService.initSetMarketupBean(agentUserId,
					Arrays.asList(rootUserId));
			bean.setOnSaleType(2);
			SetMarkupConfigCall call = GodmchManageCallService.SetMarketupBeanCall(bean);
			// 验证配置与数据库是否正确
			MarkupConfig markupConfig = new MarkupconfigWapper().selectbyPrimaryKey(agentUserId, rootUserId).get(0);
			Logger.verifyEquals(markupConfig.getMarkupType(), 1, "验证markupType");
			Logger.verifyEquals(markupConfig.getPercentage().intValue(), 33, "验证percentage");
			Logger.verifyEquals(markupConfig.getDecimalType(), 1, "验证decimalType");
			Logger.verifyEquals(markupConfig.getOnSaleType(), 2, "验证onSaleType");
			Logger.verifyEquals(markupConfig.getRefreshProduct(), true, "验证refreshProduct");
			Logger.verifyEquals(markupConfig.getConfigStatus(), 0, "验证configStatus");

			// 添加一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId, "",
					true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			List<ProductListDto> getProductLists = AddAgentProductCallService.GetProductLists(rootUserId);
			System.out.println(getProductLists);
			List<ProductListDto> getProductLists1 = AddAgentProductCallService.GetProductLists(agentUserId);
			System.out.println(getProductLists1);
			Logger.verifyEquals(getProductLists1.size(), 0, "没有自动导入");

			autoimportproductBean.setUserId(agentUserId);
			autoimportproductBean.setParentProductId(productId);
			autoimportproductCall.setData(autoimportproductBean);
			autoimportproductCall.callService();
			Logger.verifyEquals("200", autoimportproductCall.getString("code"), "验证code");
			Thread.sleep(3000);
			List<ProductListDto> getProductLists2 = AddAgentProductCallService.GetProductLists(agentUserId);

			ProductListDto agentProd = getProductLists2.get(0);
			double agentprice = agentProd.getCatalogList().get(0).getAgentPrice();
			// 代理价*1.33=直销价
			Logger.verifyEquals(new Long(Math.round(agentprice * 1.33)).intValue(),
					new Double(agentProd.getCatalogList().get(0).getDirectPrice()).intValue(), "");
			
			//商品打标
			Logger.verifyEquals(agentProd.getAutoMarkupStatus(),1,"已经被打上自动加价的标");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}